<?php
/**
 * 用户中心控制器
 */
 class MemberController extends CommonController{
 	private $Model;
	public function __init(){
		parent::__init();
		$this->Model =K('Member');
	}
	//用户管理中心首页
	 public function index(){
	 	 	//调用用户信息 加载显示用户信息
	 	$uid =  $_SESSION['uid'];
		$userInfo = M('user')->where("uid = '{$uid}'")->find();
		if($userInfo['img_url'] == ''){
			$userInfo['img_url'] = 'defaultUserImg.jpg';
		}
		$this->assign('userInfo',$userInfo);
	 	$this->dis('userInfo');
	 }
	 
	 //用户个人信息
	 public function userInfo(){
	 	//调用用户信息 加载显示用户信息
	 	$uid =  $_SESSION['uid'];
		$userInfo = M('user')->where("uid = '{$uid}'")->find();
		if($userInfo['img_url'] == ''){
			$userInfo['img_url'] = 'defaultUserImg.jpg';
		}
		$this->assign('userInfo',$userInfo);
	 	$this->dis('userInfo');
	 }
	 //修改用户资料
	 public function userInfoEdit(){
	 	if(IS_POST){
	 		if(!$this->Model->userInfoEdit()){
	 			$this->error($this->Model->error);
	 		}
			$this->success('保存成功');
	 	}
	 }
	 
	 //异步保存系统头像
	 public function savaUserImg(){
	 	if(IS_POST){
			$data['img_url'] = basename($_POST['img']);
			$useName = trim($_POST['username']);
			$info = $this->Model->where("username = '{$useName}'")->update($data);
			if($info){
				$this->ajax(1);
			}else{
				$this->ajax('0');
			}
	 	}
	 }
	 
	 	 //修改用户头像
	 public function uploadUserImg(){
			$upload = new Upload('upload/goods/'. date('y/m/d'));
			$file = $upload->upload();
			//缩略图 120X120
			$img = new image();		
			$img->thumb($file[0]['path'],'./Template/default/images/touxiang/'.$file[0]['basename'],120,120,6);
			$data = $file[0];
			$data['userimg'] = $file[0]['basename'];
			$this->ajax($data);
	 }
	 
	 	 //修改用户密码
	 public function editPassword(){
	 	if(IS_POST){
			if(!$this->Model->editPassword()){
				$this->error($this->Model->error);
			}
			$this->success('修改成功');
	 	}
	 	
	 }
	 
	 //用户订单
	 public function userOrder(){
	 	$uid = $_SESSION['uid'];
	 	//查询分配显示用户订单信息
	 	//未支付
	 	$orderBefore = M('user_order')->JOIN("__user_order__ o join __order_list__ list join __goods__ g on o.oid = list.order_id AND list.goods_gid = g.gid")->where("user_uid = '{$uid}' and order_status = 0")->group("oid")->all();//未支付订单
	 	//支付完成 、待收获 、确认货
	 	$orderafter =  M('user_order')->JOIN("__user_order__ o join __order_list__ list join __goods__ g on o.oid = list.order_id AND list.goods_gid = g.gid")->where("user_uid = '{$uid}' and order_status = 1 or order_status = 2")->group("oid")->all();
		//评论
		$orderend = M('user_order')->JOIN("__user_order__ o join __order_list__ list join __goods__ g on o.oid = list.order_id AND list.goods_gid = g.gid")->where("user_uid = '{$uid}' and order_status = 3")->group("oid")->all();//未支付订单
	 	$this->assign('orderafter',$orderafter);
		$this->assign('orderBefore',$orderBefore);
		$this->assign('orderend',$orderend);
	 	$this->dis('userOrder');
	 }
	
	//确认收货
	public function suregoods(){
		if(IS_AJAX){
			$uid= $_SESSION['uid'];
			$oid = Q('post.oid');
			//改变状态3为确认收货
			$data['order_status'] = 3;
			//写入数据库
			M('user_order')->where("user_uid = '{$uid}' and oid ='{$oid}'")->update($data);
			$this->ajax('1');
		}
	}
	
	//取消订单
	public function cancelOrder(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			$cancelOrder = Q('post.orderNums');
			//删除订单表
			M("user_order")->where("user_uid = '{$uid}' and oid = '{$cancelOrder}'")->del();
			//删除订单列表
			M("order_list")->where("order_id = '{$cancelOrder}'")->del();
			//返回信息
			$this->ajax('1');
		}
	}
	
	//用户地址管理
	public function userAddress(){
		//查询分配用户地址信息
		$uid = $_SESSION['uid'];
		$address = M("user_address")->where("user_uid ='{$uid}'")->all();
		$this->assign('address',$address);
		$this->dis('userAddress');
	}
	
	//设置默认地址
	public function addressDefault(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			$addressid = Q('post.addressid');
			//先修改所有为初始转台
			$data['user_address_isdefault'] = 0;
			M('user_address')->where("user_uid = '{$uid}'")->update($data);
			//再修改制定条记录
			$data['user_address_isdefault'] = 1;
			M('user_address')->where("user_uid = '{$uid}' and address_id='{$addressid}'")->update($data);
			//返回信息
			$this->ajax('1');
		}
	}
	
	//用户地址修改
	public function userAddressEdit(){

		$this->dis('userAddressEdit');
	}
	
	//修改地址 异步请求数据
	public function addressEdit(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			$addressid = Q('post.addressid');
			$address = M('user_address')->where("user_uid = '{$uid}' and address_id='{$addressid}'")->find();
			$this->ajax($address);
		}
	}
	
	//异步修改地址信息
	public function updateAddress(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			$addressid = Q('post.addressid');
			//组合用户地址信息 
			$data['user_address_name'] =Q('post.order_receiver');
			$data['user_address_tel'] =Q('post.tel');
			$data['user_address_phone'] =Q('post.phone');
			$data['user_address_all'] =Q('post.order_address');
			$data['user_address_place'] =Q('post.place');			
			$data['user_uid']= $uid;
			//写入数据库
			M('user_address')->where("user_uid= '{$uid}' and address_id = '{$addressid}'")->update($data);
			//返回地址id
			$this->ajax($data);
		}
	}
	
	//异步删除地址
	public function delAddress(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			$addressid = Q('post.addressid');
			M('user_address')->where("user_uid= '{$uid}' and address_id = '{$addressid}'")->del();
			$this->ajax('1');
		}
	}
	//用户收藏
	public function userFavorites(){
		header("content-type:text/html;charset=utf-8");
		$uid= $_SESSION['uid'];
		//查询用户收藏商品id
		$userInfo = M('user')->where("uid ='{$uid}'")->find();
		$collect = explode(',', $userInfo['collect']);
		$collect = array_filter($collect);
		if(!empty($collect)){
			foreach($collect as $k => $v){
				$goodsData[] = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("gid ='{$v}'")->find();
				 $goodsData[$k]['imgurl']= strstr($goodsData[$k]['goods_info_smallIMg'],',',true);
			}
			$this->assign('goodsData',$goodsData);
		}

		$this->dis('usercollect');
	}
	
	//清空用户收藏
	public function clearUsercollect(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			//过滤符号
			$gid= trim(Q('post.goodsKey'),',');
			//转数组
			$gid = explode(',',$gid);
			//查询用户信息
			$userInfo = M('user')->where("uid = '{$uid}'")->find();
			//将用户收藏信息转数组
			$userCollectId = explode(',', $userInfo['collect']) ;
			//循环比对如果相同就赋值空
			foreach($userCollectId as $k => $v){
				foreach($gid as $ke => $va){
					if($v == $va){
						$userCollectId[$k]='';
					}
				}
			}
			//过滤数组空值
			$userCollectId = array_filter($userCollectId);
			//重转回字符串
			$data['collect'] = implode(',', $userCollectId);
			//写入数据库
			M('user')->where("uid='{$uid}'")->update($data);
			//返回信息
			$this->ajax('1');
		}
	}
	
	//发布评论
	public function comment(){
		if(IS_AJAX){
			$data['comment_addTime'] = time();
			$data['comment_content'] = Q('post.con');
			$data['comment_start'] = Q('post.star');
			$data['user_uid'] = Q('post.uid');
			$data['goods_gid'] =  Q('post.gid');
			M('comment')->add($data);
			$this->ajax('1');
		}
	}
	
		 
	 //用户评论
	 public function usercomment(){
	 	//查询分配 评论信息 商品信息 
	 	$uid = $_SESSION['uid'];
	 	$comment = M()->JOIN("__comment__ c join __goods__ g on g.gid=c.goods_gid")->where("user_uid='{$uid}'")->all();
		$this->assign('comment',$comment);
	 	$this->dis('usercomment');
	 }
	 
	 //用户提问
	 public function userquestion(){
	 	//查询分配信息
	 	$uid = $_SESSION['uid'];
	 	$questionData = M()->JOIN("__goods__ g join __question__ q on q.goods_gid = g.gid ")->where("question_question_uid='{$uid}'")->all();
		$this->assign('questionData',$questionData);
	 	$this->dis('userquestion');
	 }
}